<!DOCTYPE html>
<!--
Copyright 2018 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="/tracing/metrics/metric_registry.html">
<link rel="import" href="/tracing/metrics/rendering/cpu_utilization.html">
<link rel="import" href="/tracing/metrics/rendering/frame_segment_filter.html">
<link rel="import" href="/tracing/metrics/rendering/image_decode_time.html">
<link rel="import" href="/tracing/metrics/rendering/pixels.html">
<link rel="import" href="/tracing/metrics/rendering/queueing_duration.html">

<script>
'use strict';

tr.exportTo('tr.metrics.rendering', function() {
  // Various tracing events.
  const GESTURE_EVENT = 'SyntheticGestureController::running';

  function renderingMetric(histograms, model) {
    const chromeHelper = model.getOrCreateHelper(
        tr.model.helpers.ChromeModelHelper);
    if (!chromeHelper) return;

    let segments = chromeHelper.telemetryHelper.irSegments;
    if (segments.length === 0) {
      segments = chromeHelper.telemetryHelper.animationSegments;
    }
    if (segments.length > 0) {
      const frameSegments = tr.metrics.rendering.computeFrameSegments(model, segments);
      if (frameSegments.length > 0) {
        tr.metrics.rendering.addCpuSegmentCostHistograms(
            histograms, model, frameSegments,
            (thread, segment) => thread.getCpuTimeForRange(segment.boundsRange),
            category => `thread_${category}_cpu_time_per_frame`,
            'CPU cores of a thread group per frame');
        tr.metrics.rendering.addCpuSegmentCostHistograms(
            histograms, model, frameSegments,
            (thread, segment) =>
            thread.getNumToplevelSlicesForRange(segment.boundsRange),
            category => `tasks_per_frame_${category}`,
            'Number of tasks of a thread group per frame');
        tr.metrics.rendering.addCpuWallTimeHistogram(histograms, model, frameSegments);
      }

      tr.metrics.rendering.addImageDecodeTimeHistograms(histograms, model,
          segments);
      tr.metrics.rendering.addPixelsHistograms(histograms, model, segments);
      tr.metrics.rendering.addQueueingDurationHistograms(
          histograms, model, segments);
    }
  }

  tr.metrics.MetricRegistry.register(renderingMetric, {
    requiredCategories: ['benchmark', 'toplevel'],
  });

  return {
    renderingMetric,
  };
});
</script>
